import config from "../../../config/config";
import Crypt from "../../../utils/Crypt";
import NewsModel from "../../../models/News";

const newsModel = new NewsModel()

Page({
  data: {
    page: 0,
    pageSize: 10,
    list: [],
    busy: false,
    baseUrl: config.app_base_url
  },
  onLoad: function (options) {
    this._getList()
  },

  onReachBottom() {
    this._getList(true)
  },

  _getList(append = false) {
    if (this.data.busy === false) {
      this.setData({
        busy: true,
        page: this.data.page + 1
      })
      newsModel.getList({ ...this.data }).then(response => {
        let { data } = response
        data = Crypt.parse(data)
        if (data.list.length === 0) {
          this.setData({ busy: true })
        } else {
          this.setData({ busy: false })
          if (append) {
            this.setData({ list: this.data.list.concat(data.list) })
          } else {
            this.setData({ list: data.list })
          }
        }


      })
    }
  },

  handleClick(event) {
    const item = event.currentTarget.dataset.item
    wx.navigateTo({
      url: '/sub_common/pages/news-view/index?id=' + item.id
    })
  }

});
